<%= javascript_include_tag "prototype" %>
<%= javascript_include_tag "util" %>
<%= javascript_include_tag "calendar" %>
<html>
    <head>
        <title>选择产品</title>
        <link href="/stylesheets/framework.css" media="screen" rel="Stylesheet" type="text/css" />
        <!--Start-->
        <%= javascript_include_tag "prototype" %>
        <script type="text/javascript">
            var url=location.href;
            var args=url.substring(url.indexOf("?")+1,url.length).split("&");
            var oTmpGoods = new Array();
            for (var i=0; i < args.length; i++) {
                oTmpGoods[i] = new Array();
                oTmpGoods[i] = args[i].split("=");
            }
var oGoods=new Array();
function queryProduct(){
    var url = '/ajax/get_product';
    var pars = 'product_name=' + encodeURI($F('product_name')) + '&product_serial=' + $F('product_serial');
    var myAjax = new Ajax.Request(url,{method: 'get', parameters:pars, onComplete: showResponse});
}
function showResponse(originalRequest){
    var oXML = originalRequest.responseXML;   
    if (oXML==null){
        alert("无法载入产品信息,请稍候再试.");
        return 0;
    }
    var oNodeList=oXML.getElementsByTagName("goods");
    var nodeLength=oNodeList.length;
    if (nodeLength==0){
        alert("没有查到相关产品,请重新检索.");
        return 0;
    }
    var sHTML="";
    for(var i=0;i<nodeLength;i++){
        var oNode=oNodeList[i];
        oGoods[i]={	id: oNode.getAttribute("id"),
            serial: oNode.getAttribute("serial"),
            name: oNode.getAttribute("name"),
            F: oNode.getAttribute("F"),
            B: oNode.getAttribute("B"),
            P: oNode.getAttribute("P"),
            total_counts: oNode.getAttribute("total_counts"),
            in_date_at: oNode.getAttribute("in_date_at"),
            sale_price: oNode.getAttribute("sale_price"),
            count: oNode.getAttribute("outcount")
        }

        for (var j=0; j<oTmpGoods.length; j++) {
            oTmpGoods[j][0] = oTmpGoods[j][0].replace(/\%20/g,' ');
            if (oTmpGoods[j][0] == oGoods[i].serial) {
                oGoods[i].count -= (parseFloat(oTmpGoods[j][1]).toFixed(3));
            }
        }

        sHTML	+='<tr id="LIST_TB_CONTENT2">'
            + '<td>'+oGoods[i].serial+'</td>'
            + '<td>'+oGoods[i].name+'</td>'
            + '<td>'+oGoods[i].F+'</td>'
            + '<td>'+oGoods[i].B+'</td>'
            + '<td>'+oGoods[i].P+'</td>'
            + '<td>'+oGoods[i].total_counts+'</td>'
            + '<td>'+oGoods[i].in_date_at+'</td>'
            + '<td>'+oGoods[i].sale_price+'</td>'
            + '<td>'+parseFloat(oGoods[i].count).toFixed(3)+'</td>'
            + '<td><input type="radio" name="rdoSelectGoods" id="rdoSelectGoods'+i+'" onclick="changePrice('+i+')"/></td>'
            + '</tr>';
    }
    $('divGoodsDetail').innerHTML	='<table id="LIST_TB" border="0">'
        +'<tbody>'
        +'<tr id="LIST_TB_TITLE">'
        +'<td nowrap="nowrap">品名规格</td>'
        +'<td nowrap="nowrap">导针规格</td>'
        +'<td nowrap="nowrap">F（槽宽）</td>'
        +'<td nowrap="nowrap">B（中径）</td>'
        +'<td nowrap="nowrap">P（脚距）</td>'
        +'<td nowrap="nowrap">入库数量（kpcs）</td>'
        +'<td nowrap="nowrap">入库日期</td>'
        +'<td nowrap="nowrap">单价</td>'
        +'<td nowrap="nowrap">可销售量（kpcs）</td>'
        +'<td colspan="3">选择</td>'
        +'</tr>'
        +sHTML;
    +'</tbody>'
        +'</table>';
    $("rdoSelectGoods0").checked=true;
    $("divReturn").style.display="block";
    $("sale_price").value=oGoods[0].sale_price;
}
function checkPrice(flag, msg_id, obj_id, msg_text){
        //debugger
        if (flag) {
                if ($(obj_id).value != "" && ! yuanCheck($(obj_id).value))
                {
                        $(msg_id).innerText = msg_text;
                        $(msg_id).style.display = "block";
                        return false;
                }
                else return true;
        }
        else {
                $(msg_id).style.display = "none";
        }
}
function returnProduct(){
    if ($F("sale_count")=="" || parseFloat($F("sale_count")) <= 0){
        alert("销售数量不能为空,请输入销售数量.");
        $("sale_count").focus();
        return 0;
    }
    if ($F("sale_price")=="" || parseFloat($F("sale_price")) <= 0){
            alert("销售价格不能为空,请输入销售价格.");
            $("sale_price").focus();
            return 0;
    }
    /*else {
            parseFloat($F("sale_price"))=parseFloat($F("sale_price")).toFixed(2)
    }*/
    if ($F("memo")=="") $("memo").innerHTML="&nbsp;";
    var i;
    var l=oGoods.length;
    for (i=0;i<l;i++)
        if ($("rdoSelectGoods"+i).checked==true) break;
    if (parseFloat($F("sale_count"))>parseFloat(oGoods[i].count)){
        alert("销售数量不能大于可销售量.");
        $("sale_count").focus();
        return 0;
    }
    if ($F("goods_num") == "" || parseFloat($F("goods_num")) <= 0 ){
        alert("件数不能为空.");
        $("goods_num").focus();
        return 0;
    }

    if (window.opener!=null) window.opener.addGoods(oGoods[i].id,oGoods[i].serial,oGoods[i].name,parseFloat($F("sale_count")).toFixed(3),parseFloat($F("sale_price")).toFixed(2),oGoods[i].sale_price,$F("memo"),oGoods[i].F,oGoods[i].B,oGoods[i].P,parseFloat($F("goods_num")).toFixed(1));
    window.close();
}
function changePrice(i){
    $("sale_price").value=oGoods[i].sale_price;
}
function totalIsNumber(e){
   if (!e){
        if ((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 && event.keyCode!=8)
            event.returnValue=false;
        else if(event.keyCode==46 && $F("sale_count").search(/\./)!=-1)
            event.returnValue=false;
    }
    else{
        if ((e.which<48 || e.which>57) && e.which!=46 && e.which != 8)
            return false;
        else if(e.which==46 && $F("sale_count").search(/\./)!=-1)
            return false;
    }
}
function priceIsNumber(e){
    if (!e){
        if ((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 && event.keyCode!=8)
            event.returnValue=false;
        else if(event.keyCode==46 && $F("sale_price").search(/\./)!=-1)
            event.returnValue=false;
    }
    else{
        if ((e.which<48 || e.which>57) && e.which!=46 && e.which != 8)
            return false;
        else if(e.which==46 && $F("sale_price").search(/\./)!=-1)
            return false;
    }
}
function goodsNumIsNumber(e){
    if (!e){
        if ((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 && event.keyCode!=8)
            event.returnValue=false;
        else if(event.keyCode==46 && $F("goods_num").search(/\./)!=-1)
            event.returnValue=false;
    }
    else{
        if ((e.which<48 || e.which>57) && e.which!=46 && e.which != 8)
            return false;
        else if(e.which==46 && $F("goods_num").search(/\./)!=-1)
            return false;
    }
}

function pageLoad(){
    $("sale_count").onkeypress=totalIsNumber;
    $("goods_num").onkeypress=goodsNumIsNumber;
    $("sale_price").onkeypress=priceIsNumber;
}

</script>
<!--End-->
        </head>
        <body onLoad="pageLoad()">
            <form id="input-form" action="/sale/select_product" method="post">
                <div style="height: 25px;"></div>
                <div style="text-align: center;">
                    <input name="optype" value="search" type="hidden">
                    品名规格：<input id="product_serial" type="text" maxlength="20"/>&nbsp;&nbsp;&nbsp;
                    导针规格：<input id="product_name" type="text" maxlength="20"/>&nbsp;<!-- Start --><input value="查询" class="sub" type="button" onclick="queryProduct();" /><!-- End -->

                </div>
            </form>

            <input name="optype" value="select" type="hidden">
            <!-- Start -->
            <div id="divGoodsDetail"></div>
            <div style="height: 10px;"></div>
            <div id="divReturn" style="display: none;">
                <table border="1" align="center">
                    <tbody>
                        <tr id="LIST_TB_CONTENT1">
                            <td>销售数量:</td>
                            <td><input id="sale_count" type="text" maxlength="20"/>（kpcs）</td>
                        </tr>
                        <tr id="LIST_TB_CONTENT1">
                            <td>件数:</td>
                            <td><input id="goods_num" type="text" maxlength="20" value="1"/></td>
                        </tr>
                        <tr id="LIST_TB_CONTENT1">
                            <td>销售价格:</td>
                            <td><input id="sale_price" type="text"  name ="sale_price" maxlength="20" onfocus="checkPrice(false,'id_sale_price')" onblur="checkPrice(true,'id_sale_price',this.id,'价格输入错误')" />（元）<div class="redNsg" id="id_sale_price" style="display:none;"></div></td>
                        </tr>
                        <tr id="LIST_TB_CONTENT1">
                            <td>备注:</td>
                            <td><textarea cols="50" rows="5" id="memo"></textarea></td>
                        </tr>
                </tbody></table>
                <div style="text-align: center;"><input type="button" class="sub" value="确定" onclick="returnProduct()" /></div>
                <!-- End -->
            </div>
            <div style="height: 10px;"></div>
    </body></html>
